﻿@using Project.Engine.Utilities;
@using System.Web.Mvc;
@{
    Type type = ((object)this.Model).GetType();

    int TotalRecord = type.GetProperty("TotalRecord") != null ? ConvertUtil.ToInt32(type.GetProperty("TotalRecord").GetValue(this.Model, null), 0) : 0;
    int PageSize = type.GetProperty("PageSize") != null ? ConvertUtil.ToInt32(type.GetProperty("PageSize").GetValue(this.Model, null), 0) : 0;
    int CPage = type.GetProperty("CPage") != null ? ConvertUtil.ToInt32(type.GetProperty("CPage").GetValue(this.Model, null), 0) : 0;
    int cateId = type.GetProperty("CateId") != null ? ConvertUtil.ToInt32(type.GetProperty("CateId").GetValue(this.Model, null), 0) : 0;
    string key = type.GetProperty("Key") != null ? ConvertUtil.ToString(type.GetProperty("Key").GetValue(this.Model, null), string.Empty) : string.Empty;
    string listType = type.GetProperty("ListType") != null ? ConvertUtil.ToString(type.GetProperty("ListType").GetValue(this.Model, null), string.Empty) : string.Empty;
    int id = type.GetProperty("id") != null ? ConvertUtil.ToInt32(type.GetProperty("id").GetValue(this.Model, null), 0) : 0;
    int postId = type.GetProperty("PostId") != null ? ConvertUtil.ToInt32(type.GetProperty("PostId").GetValue(this.Model, null), 0) : 0;
    string keyword = type.GetProperty("keyword") != null ? ConvertUtil.ToString(type.GetProperty("keyword").GetValue(this.Model, null), string.Empty) : string.Empty;

    int pagecount = (int)Math.Ceiling((decimal)TotalRecord / PageSize);


    if (pagecount > 1)
    {
        int begin = CPage - 3;
        begin = begin > 0 ? begin : 1;
        int end = begin + 7;
        begin = end >= pagecount ? pagecount - 7 : begin;
        begin = begin > 0 ? begin : 1;
        end = end >= pagecount ? pagecount : end;
            <div class="pagination">
                @if (CPage == 0) {
                    <span class="disabled">&lt;&lt;</span>
                }
                @if (CPage > 1) {
                    <a href="?@(CPage - 1)">&lt;&lt;</a>
                }
                @for (int i = begin; i < end; i++)
                {
                    if (i == CPage || (CPage == 0 && i == begin))
                    {
                    <span class="current">@i</span> 
                    }
                    else
                    {
            <a href="?@i">@i</a>
                    }
                }
                @if (CPage < pagecount - 1)
                {
                    <a href="?@(CPage + 1)">&gt;&gt;</a>
                }
            </div>
    }
}
